package com.bnz.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
// @WebFilter(urlPatterns = "/*")
public class CharacterEncodingFilter implements Filter {

	//1. 初始化方法，只执行一次
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		System.out.println("CharacterEncodingFilter---》init()");
	}
	//2. 每次请求都会执行，执行多次
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		System.out.println("CharacterEncodingFilter---》doFilter()");
		//2.1 处理post请求的中文乱码
		request.setCharacterEncoding("UTF-8");
		//2.2 设置响应的页面字符集编码
		response.setContentType("text/html;charset=utf8");
		//2.3 放行过滤器
		chain.doFilter(request,response);
	}
	//3. 销毁过滤器时执行，执行一次
	@Override
	public void destroy() {
		System.out.println("CharacterEncodingFilter---》destroy()");
	}
}
